<?php

use console\db\Migration;
use yii\db\Expression;
use yii\helpers\Console;

/**
 * 格式化用户表中相关字段内容
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300000_000002_t_user_format extends Migration {

    /**
     * @inheritdoc
     */
    public $tableName = '{{%user}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        //原正常用户状态变更
        Console::output("update {$this->tableName} [`status`>=0] to [10] running...");
        $this->update($this->tableName, ['status' => 10], ['>=', 'status', 0]);
        Console::output("update done.");
        //原禁用状态变更
        Console::output("update {$this->tableName} [`status`<0] to [0] running...");
        $this->update($this->tableName, ['status' => 0], ['<', 'status', 0]);
        Console::output("update done.");
        //原注册时间为0的使用更新时间
        Console::output("update {$this->tableName} [`created_at`=0] to [`updated_at`] running...");
        $this->update($this->tableName, ['created_at' => new Expression('updated_at')], ['created_at' => 0]);
        Console::output("update done.");
        //原注册时间为0的使用五年前时间戳
        Console::output("update {$this->tableName} [`created_at`=0] to [-5 year] running...");
        $this->update($this->tableName, ['created_at' => strtotime('-5 year')], ['created_at' => 0]);
        Console::output("update done.");
        //原更新时间为0的使用注册时间
        Console::output("update {$this->tableName} [`updated_at`=0] to [`created_at`] running...");
        $this->update($this->tableName, ['updated_at' => new Expression('created_at')], ['updated_at' => 0]);
        Console::output("update done.");
    }

}
